import math

from flask import Flask, render_template, request
import sqlite3
import datetime


app = Flask(__name__)


@app.route('/')
def index():
    return render_template('index.html')


@app.route('/wordcloud')
def wordcloud():
    return render_template('wordcloud.html')


@app.route('/list')
def movie():
    datalist = []
    con = sqlite3.connect("MeiJu.db")
    cur = con.cursor()
    sql = "select * from MeiJu"
    data = cur.execute(sql)
    for item in data:
        datalist.append(item)
    cur.close()
    con.close()
    return render_template('movie.html', movies=datalist)


@app.route('/page')
def page():
    datalist = []
    p = 1
    show_shouye_status = 1
    con = sqlite3.connect("MeiJu.db")
    cur = con.cursor()
    sql = "select * from MeiJu"
    data = cur.execute(sql)
    for item in data:
        datalist.append(item)
    cur.close()
    con.close()
    total = int(math.ceil(datalist[0]/36))

    dic = get_page(total)

    datas = {
        'user_list': datalist,
        'p': int(p),
        'total': total,
        'show_shouye_status': show_shouye_status,
        'dic_list': dic

    }

    return render_template('pageSplit.html', datas=datas)


def get_page(total, p):
    """get page 封装"""
    show_page = 5  # 显示的页码数
    pageoffset = 2  # 偏移量
    start = 1  # 分页条开始
    end = total  # 分页条结束

    if total > show_page:
        if p > pageoffset:
            start = p - pageoffset
            if total > p + pageoffset:
                end = p + pageoffset
            else:
                end = total
        else:
            start = 1
            if total > show_page:
                end = show_page
            else:
                end = total
        if p + pageoffset > total:
            start = start - (p + pageoffset - end)
    # 用于模版中循环
    dic = range(start, end + 1)
    return dic


@app.route('/score')
def score():
    score = []
    num = []
    con = sqlite3.connect("MeiJu.db")
    cur = con.cursor()
    sql = "select score,count(score) from MeiJu group by score"
    data = cur.execute(sql)
    for item in data:
        score.append(item[0])
        num.append(item[1])
    cur.close()
    con.close()
    # s = [
    #     6.2,
    #     6.4,
    #     6.5,
    #     6.8,
    #     6.9,
    #     7.1,
    #     7.2,
    #     7.3,
    #     7.4,
    #     7.5,
    #     7.6,
    #     7.7,
    #     7.8,
    #     7.9,
    #     8.1,
    #     8.2,
    #     8.4,
    #     8.5,
    #     8.6,
    #     8.7,
    #     8.8,
    #     8.9,
    #     9.1,
    #     9.2
    # ]
    s = list(score.pop(-1))
    # s = score.pop()
    return render_template("score.html", score=score, num=num)


if __name__ == '__main__':
    app.run()
